const express=require('express');
const mysql=require('mysql');
const urlobj=require('url');
//引用自定义
//const commte=require('../../libs/commtle');
//链接数据库
var db=mysql.createPool({
    host:'localhost',
    user:'root',
    password:'',
    database:'leasedb'
});

//业务逻辑
module.exports = function() {
    var router = express.Router();

    //接收页面传过来的id值
    router.get('/updatefouram.html',function (req,res,next) {
        //创建要执行的SQL语句
        db.query('SELECT * FROM forumainfo WHERE forumAId='+req.query.forumAId,function (err,data) {

            //判断数据库是否执行成功
            if(err){
                res.status(400).send('<script>alert("数据查询失败，请检查后再次查询!");window.location.href="404.html";</script>').end();
            }
            else{
                if(data.length==0){
                    res.status(400).send('<script>alert("暂无数据!");window.location.href="404.html";</script>').end();
                }
                else{

                    res.render("forum/updatefouram.ejs",{"msg":data});
                }
            }
        });
    });
    //点击确定修改
    router.post('/updatefouram',function (req,res,next) {
        console.log(req.body);

        db.query('UPDATE forumainfo SET forumAName="'+req.body.fouramname+'" WHERE forumAId='+parseInt(req.body.fouramId),function (err,data) {
            if(err){
                res.status(400).send('<script>alert("修改执行失败，请稍后再次执行!");window.location.href="404.html";</script>').end();
            }
            else{
                if(data.length==0){
                    res.status(400).send('<script>alert("修改失败!");window.location.href="404.html";</script>').end();
                }
                else{
                    res.status(200).send('<script>alert("修改成功!");window.location.href="PrimaryClassification.html";</script>')
                }
            }
        })
    });
//开始渲染后台首页
    router.get('/updatefouram.html',function(req,res,next){
        res.render("forum/updatefouram.ejs");
    });

    return router;
}